<html>
<head><meta charset="utf-8"><title>async closures · wg-async-foundations · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/index.html">wg-async-foundations</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/async.20closures.html">async closures</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="199139298"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/async%20closures/near/199139298" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Yoshua Wuyts <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/async.20closures.html#199139298">(May 29 2020 at 11:02)</a>:</h4>
<p>Running into a really fun interaction in Tide because of both the lack of async closures and the lack of async traits: <a href="https://github.com/http-rs/tide/pull/556">https://github.com/http-rs/tide/pull/556</a></p>



<a name="199139477"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/async%20closures/near/199139477" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Yoshua Wuyts <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/async.20closures.html#199139477">(May 29 2020 at 11:04)</a>:</h4>
<p>We would want to write:</p>
<div class="codehilite"><pre><span></span><code><span class="c1">// this example is not possible</span>
<span class="n">app</span><span class="p">.</span><span class="n">middleware</span><span class="p">(</span><span class="o">|</span><span class="n">req</span><span class="p">,</span><span class="w"> </span><span class="n">next</span><span class="o">|</span><span class="w"> </span><span class="k">async</span><span class="w"> </span><span class="k">move</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">next</span><span class="p">.</span><span class="n">run</span><span class="p">(</span><span class="n">req</span><span class="p">).</span><span class="k">await</span><span class="w"> </span><span class="p">});</span><span class="w"></span>
</code></pre></div>



<a name="199139672"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/async%20closures/near/199139672" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Yoshua Wuyts <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/async.20closures.html#199139672">(May 29 2020 at 11:07)</a>:</h4>
<p>But because <code>async fn</code> in traits is only possible by returning a <code>Pin&lt;Box&lt;Future&gt;&gt;</code> <em>and</em> we don't have an <code>AsyncFn</code> definition we cannot express that yet. So instead we're working around it by defining custom structs.</p>



<a name="199139784"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/async%20closures/near/199139784" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Yoshua Wuyts <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/async.20closures.html#199139784">(May 29 2020 at 11:08)</a>:</h4>
<p>Figured this could be a an interesting example to showcase the limitations of async/await in Rust currently.</p>



<a name="199707245"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/async%20closures/near/199707245" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benjamin Saunders <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/async.20closures.html#199707245">(Jun 04 2020 at 05:44)</a>:</h4>
<p>while traits are certainly an issue, you don't need <code>AsyncFn</code>:</p>
<div class="codehilite"><pre><span></span><code>use std::future::Future;

fn foo&lt;T: Future&lt;Output=i32&gt;, F: Fn() -&gt; T&gt;(x: F) {}

fn main() {
    foo(|| async { 42 })
}
</code></pre></div>



<a name="201047017"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/async%20closures/near/201047017" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Yoshua Wuyts <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/async.20closures.html#201047017">(Jun 16 2020 at 17:17)</a>:</h4>
<p><span class="user-mention" data-user-id="216097">@Benjamin Saunders</span> in this case the <code>Next</code> argument is borrowed, which is where the need for <code>AsyncFn</code> came from -- probably should've clarified <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span></p>



<a name="201047150"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/async%20closures/near/201047150" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benjamin Saunders <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/async.20closures.html#201047150">(Jun 16 2020 at 17:19)</a>:</h4>
<p>hmm, how would that help? don't async fns always borrow all their arguments?</p>



<a name="201088887"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/async%20closures/near/201088887" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benjamin Saunders <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/async.20closures.html#201088887">(Jun 17 2020 at 00:06)</a>:</h4>
<p>a regular closure at least has the option of constructing a non-borrowing future</p>



<a name="201491235"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/async%20closures/near/201491235" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Laban <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/async.20closures.html#201491235">(Jun 20 2020 at 18:46)</a>:</h4>
<p><span class="user-mention" data-user-id="216097">@Benjamin Saunders</span> async fns can take ownership of their arguments just fine. Borrowing is harder. I added some notes about this on <a href="https://github.com/rust-lang/rust/issues/45994#issuecomment-633144274">https://github.com/rust-lang/rust/issues/45994#issuecomment-633144274</a> and I'd like to have a go at solving it. Is anyone available to mentor me on this?</p>



<a name="201492566"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/async%20closures/near/201492566" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benjamin Saunders <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/async.20closures.html#201492566">(Jun 20 2020 at 19:12)</a>:</h4>
<p>yes, of course they take ownership of values; what they can't do is <em>not</em> borrow a <em>reference</em></p>



<a name="201492593"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/async%20closures/near/201492593" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benjamin Saunders <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/async.20closures.html#201492593">(Jun 20 2020 at 19:13)</a>:</h4>
<p>whereas a regular closure can freely control the lifetime of its returned future</p>



<a name="201495421"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/async%20closures/near/201495421" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Laban <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/async.20closures.html#201495421">(Jun 20 2020 at 20:15)</a>:</h4>
<p>(I'm trying to put together a proof-of-concept implementation of the feature, but have only got as far as this: <a href="https://github.com/alsuren/rust/pull/1">https://github.com/alsuren/rust/pull/1</a> )</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>